package page;

import utils.JdbcUtils;

import java.util.List;
import java.util.Map;

public class PageClass {
    public static int sum= JdbcUtils.JdbcQuery("SELECT page_class_name FROM page_class").size();  //注册文章种类统计
    public static void jssum(){
        sum=JdbcUtils.JdbcQuery("SELECT page_class_name FROM page_class").size();  //注册文章数目统计
    }
    public static String sql_addClass="INSERT INTO page_class values ( ? , ? );";
    public static String sql_deleteClass="DELETE FROM page_class WHERE page_class_name = ?";

    public static String sql_queryClass="select * from page_class where page_class_name = ?";
    public static String sql_updataClass="UPDATE page_class SET page_class_name = ? WHERE page_class_name = ?";
    public static boolean sameName(String name){
        String sql_same_query="SELECT * FROM page_class WHERE page_class_name = "+name;
        return !JdbcUtils.JdbcQuery(sql_same_query).isEmpty();
    }
    public static void idChange(){
        String sql = "SELECT * FROM page_class ORDER BY page_class_id ASC";
        List<Map<String, Object>> rs = JdbcUtils.JdbcQuery(sql);
        int newId = 1;
        for(Map<String,Object> map : rs){
            for(String key : map.keySet()) {
                if (key.equals("page_class_id")){
                    int oldId = (int) map.get(key);
                    String updateSql = ("UPDATE page_class SET page_class_id = " + newId +" WHERE page_class_id = " + oldId);
                    JdbcUtils.JdbcUpdate(updateSql);
                    newId++;
                }
            }
        }
    }
}
